// pages/mockexam/special/single/detail/index.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        options: {},
        headList: [],
        headSize: 0,
        headIndex: 0,
        subjectList: [],
        subjectSize: 0,
        subjectIndex: 0,
        list: [],
        questionList: [],
        optionList: [],
        optionIndex: 0,
        listIndex: 0,
        listSize: 0,
        questionIndex: 0,
        qmaxSize: 0,
        qcount: 0,
        submitObj: {},
        flag: true,
        resultInfo: {},
        showButton: false,
        userInfo: {},
        theoryUserId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            options: options
        })
        wx.setNavigationBarTitle({
            title: options.title,
        })
        var userInfo = app.globalData.userInfo;
        var _this = this;
        //标题
        app.paiRequest("/app/mockexam/theory/findList", {
            parentCode: options.id
        }).then(function (res) {
            var headList = res.data;
            _this.setData({
                headList: headList,
                headSize: headList.length,
                userInfo: userInfo
            })
            //题目  
            var headObj = headList[0];
            app.paiRequest("/app/mockexam/theory/findList", {
                parentCode: headObj.id
            }).then(function (ress) {
                var subjectList = ress.data;
                _this.setData({
                    subjectList: subjectList
                })
                //问题questionList
                var questeObje = subjectList[0];
                app.paiRequest("/app/mockexam/theory/listQuestion", {
                    parentCode: questeObje.id
                }).then(function (resss) {
                    _this.setData({
                        questionList: resss.data
                    })

                })
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var userInfo = app.globalData.userInfo;
        var theoryId = this.data.options.id;
        var _this = this;
        app.paiRequest("/app/mockexam/theory/user/save", {
            userId: userInfo.id,
            theoryId: theoryId
        }).then(function (res) {
            _this.setData({
                theoryUserId: res.data.id
            })
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    nextquestion: function () {
        this.setData({
            questionIndex: this.data.questionIndex + 1
        })
    },
    upList: function () {
        var _this = this;
        var questionList = this.data.questionList;
        var questionIndex = this.data.questionIndex;
        var userInfo = this.data.userInfo;
        var obje = questionList[questionIndex - 1];
        if (obje == undefined) {
            var subjectList = this.data.subjectList;
            var subjectIndex = this.data.subjectIndex;
            if (subjectList[subjectIndex - 1] == undefined) {
                var headList = this.data.headList;
                var headIndex = this.data.headIndex;
                if (headList[headIndex - 1] == undefined) {

                } else {
                    this.setData({
                        headIndex: headIndex - 1
                    })
                    app.paiRequest("/app/mockexam/theory/findList", {
                        parentCode: headList[headIndex - 1].id
                    }).then(function (res) {
                        var subjectList = res.data;
                        _this.setData({
                            subjectList: subjectList,
                            subjectIndex: 0
                        })
                        app.paiRequest("/app/mockexam/theory/listQuestion", {
                            parentCode: subjectList[0].id
                        }).then(function (ress) {
                            var questionList = ress.data;
                            _this.setData({
                                questionList: questionList,
                                questionIndex: 0
                            })

                        })
                    })
                }
            } else {
                this.setData({
                    subjectIndex: subjectIndex - 1
                })
                app.paiRequest("/app/mockexam/theory/listQuestion", {
                    parentCode: subjectList[subjectIndex - 1].id
                }).then(function (res) {
                    var questionList = res.data;
                    _this.setData({
                        questionList: questionList,
                        questionIndex: 0
                    })

                })
            }
        } else {
            var optionIndex = _this.data.optionIndex;
            this.setData({
                questionIndex: questionIndex - 1
            })


        }
    },
    nextList: function () {
        var _this = this;
        var questionList = this.data.questionList;
        var questionIndex = this.data.questionIndex;
        var obje = questionList[questionIndex + 1];
        var userInfo = _this.data.userInfo;
        if (obje == undefined) {
            var subjectList = this.data.subjectList;
            var subjectIndex = this.data.subjectIndex;
            if (subjectList[subjectIndex + 1] == undefined) {
                var headList = this.data.headList;
                var headIndex = this.data.headIndex;
                if (headList[headIndex + 1] == undefined) {
                    this.setData({
                        showButton: true
                    })
                } else {
                    this.setData({
                        headIndex: headIndex + 1
                    })
                    app.paiRequest("/app/mockexam/theory/findList", {
                        parentCode: headList[headIndex + 1].id
                    }).then(function (res) {
                        var subjectList = res.data;
                        _this.setData({
                            subjectList: subjectList,
                            subjectIndex: 0
                        })
                        app.paiRequest("/app/mockexam/theory/listQuestion", {
                            parentCode: subjectList[0].id
                        }).then(function (ress) {
                            var questionList = ress.data;
                            _this.setData({
                                questionList: questionList,
                                questionIndex: 0
                            })

                        })
                    })
                }

            } else {
                this.setData({
                    subjectIndex: subjectIndex + 1
                })
                app.paiRequest("/app/mockexam/theory/listQuestion", {
                    parentCode: subjectList[subjectIndex + 1].id
                }).then(function (res) {
                    var questionList = res.data;
                    _this.setData({
                        questionList: questionList,
                        questionIndex: 0
                    })

                })
            }
        } else {
            this.setData({
                questionIndex: questionIndex + 1
            })

        }
    },
    radioChang: function (e) {
        var questionId = e.currentTarget.dataset.id;
        var type = e.currentTarget.dataset.type;
        var answerId = e.detail.value;
        var userInfo = this.data.userInfo;
        var options = this.data.options;
        var postId = e.currentTarget.dataset.postid;
        app.paiRequest("/app/mockexam/theory/record/saveRecord", {
            theoryId: options.id,
            userId: userInfo.id,
            questionId: questionId,
            answerId: answerId,
            questionType: type,
            postId: postId
        }).then(function (res) {
            console.log(res)
        })
    },
    checkChang: function (e) {
        var questionId = e.currentTarget.dataset.id;
        var type = e.currentTarget.dataset.type;
        var answerId = e.detail.value;
        var userInfo = this.data.userInfo;
        var options = this.data.options;
        var postId = e.currentTarget.dataset.postid;
        app.paiRequest("/app/mockexam/theory/record/saveRecord", {
            theoryId: options.id,
            userId: userInfo.id,
            questionId: questionId,
            answerId: answerId,
            questionType: type,
            postId: postId
        }).then(function (res) {
            console.log(res)
        })
    },
    submitQuestion: function () {
        var submitObj = this.data.submitObj;
        var _this = this;
        var userInfo = this.data.userInfo;
        var options = this.data.options;
        var theoryUserId = this.data.theoryUserId;
        app.paiRequest("/app/mockexam/theory/compute", {
            theoryUserId: theoryUserId,
            userId: userInfo.id,
            theoryId: options.id
        }).then(function (res) {
            console.log(res)
            var data = res.data;
            _this.setData({
                resultInfo: data,
                flag: false
            })
        })
    },
    // 遮罩层隐藏
    conceal: function () {
        this.setData({
            flag: true
        })
    },
    deleteRecord: function () {
        var userInfo = this.data.userInfo;
        var options = this.data.options;
        app.paiRequest("/app/mockexam/theory/record/delete", {
            theoryId: options.id,
            userId: userInfo.id
        }).then(function (res) {
            wx.navigateBack({
                delta: 1
            })
        })
    }
})